﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework;


namespace FXnaEngine.Graphics
{
    public class TexturedPlane
    {



        #region Private Static Method

        public static FMesh CreatePlane(float xdim, float ydim, float texCoordX, float texCoordY,
            float texCoordWidth, float texCoordHeight,GraphicsDevice device)
        {
            FMesh customMesh = new FMesh();

            VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[4];
            Vector3 halfExtent = new Vector3();

            halfExtent.X = xdim / 2;
            halfExtent.Y = ydim / 2;

            Vector3 v0 = Vector3Helper.Get(-halfExtent.X, 0, -halfExtent.Y);
            Vector3 v1 = Vector3Helper.Get(halfExtent.X, 0, -halfExtent.Y);
            Vector3 v2 = Vector3Helper.Get(-halfExtent.X, 0, halfExtent.Y);
            Vector3 v3 = Vector3Helper.Get(halfExtent.X, 0, halfExtent.Y);



            //this.vertices.Add(v0);
            //this.vertices.Add(v1);
            //this.vertices.Add(v2);
            //this.vertices.Add(v3);



            Vector3 pY = Vector3.UnitY;

            vertices[0].Position = v0; vertices[1].Position = v1;
            vertices[2].Position = v2; vertices[3].Position = v3;

            vertices[0].TextureCoordinate = new Vector2(texCoordX, texCoordY);
            vertices[1].TextureCoordinate = new Vector2(texCoordX + texCoordWidth, texCoordY);
            vertices[2].TextureCoordinate = new Vector2(texCoordX, texCoordY + texCoordHeight);
            vertices[3].TextureCoordinate = new Vector2(texCoordX + texCoordWidth, texCoordY + texCoordHeight);

            for (int i = 0; i < 4; i++)
                vertices[i].Normal = pY;

            customMesh.VertexDeclaration = VertexPositionNormalTexture.VertexDeclaration;

            customMesh.VertexBuffer = new VertexBuffer(device, typeof(VertexPositionNormalTexture),
                4, BufferUsage.None);
            customMesh.VertexBuffer.SetData<VertexPositionNormalTexture>(vertices);

            short[] indices = new short[6];

            indices[0] = 0; indices[1] = 1; indices[2] = 2;
            indices[3] = 2; indices[4] = 1; indices[5] = 3;





            customMesh.IndexBuffer = new IndexBuffer(device, typeof(short), 6,
                BufferUsage.None);
            customMesh.IndexBuffer.SetData(indices);

            customMesh.NumberOfVertices = 4;
            customMesh.NumberOfPrimitives = 2;




            return customMesh;


            //if (this.vertices.Count == 0)
            //{
            //    throw new GoblinException("Corrupted model vertices. Failed to calculate MBB.");
            //}
            //else
            //{
            //    boundingBox = BoundingBox.CreateFromPoints(this.vertices);
            //    boundingSphere = BoundingSphere.CreateFromPoints(this.vertices);
            //    if (offsetTransform.Equals(Matrix.Identity))
            //        offsetTransform.Translation = (boundingBox.Min + boundingBox.Max) / 2;
            //}
        }

        #endregion



    }
}
